(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('apf.finance.rejectPreviewController', createRejectPreviewController);
	createRejectPreviewController.$inject = [ '$scope', 'Constants', 'formatService', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService',
			'piDiscountService', 'storageService','arithService' ];
	function createRejectPreviewController($scope, Constants, formatService, modalService, gridService, layoutConfig, validatorConfig, authService, piDiscountService, storageService, arithService) {
		var ctrl = this, piDiscountRequestInfo = $scope.$stateParams.params, errorMessage = {};
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.piDiscountRequest = {};
		ctrl.piDiscountRequest.invoiceIds = ctrl.piDiscountRequest.invoiceIds || [];

		ctrl.init = function() {
			ctrl.loanGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.tableConfigs.loanTable, null);
			ctrl.loanGridOptions.data = 'vm.piDiscountRequest.assetLoanList';
			ctrl.loanGridOptions.enableRowHeaderSelection = false;
			ctrl.invoiceGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.tableConfigs.invoiceTable, null);
			ctrl.invoiceGridOptions.data = 'vm.piDiscountRequest.discountInvoiceList';
			ctrl.invoiceGridOptions.enableRowHeaderSelection = false;
			authService.createRejectPreview(piDiscountRequestInfo).then(function(response) {
				if (response.data && response.data.model) {
					var piDiscountRequest = response.data.model;
					var totalAmt = parseFloat(arithService.add(piDiscountRequest.intTotalAmt, piDiscountRequest.feeTotalAmt));
					if(arithService.eq(piDiscountRequest.loanTotalAmt, 0) || arithService.eq(totalAmt, 0)){
						piDiscountRequest.costCelling = 0;
					}else{
						piDiscountRequest.costCelling = parseFloat(arithService.mul(arithService.div(totalAmt,piDiscountRequest.loanTotalAmt),100)).toFixed(2);
					}
					angular.forEach(piDiscountRequest.discountInvoiceList, function(data) {
						if(data.communityCodeQ != null && data.communityCode != null){
							data.community = data.communityCodeQ + ":" + data.communityCode;
						}
					});
					ctrl.piDiscountRequest = piDiscountRequest;
				}
				if(piDiscountRequestInfo.taskId){
					ctrl.piDiscountRequest.taskId = piDiscountRequestInfo.taskId;
				}
			});
		};

		ctrl.createRejectPreviewSubmit = function() {
			return authService.createRejectPreviewSubmit(ctrl.piDiscountRequest).then(submitSuccessful, submitFailed);
		};
		
		function submitSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.submit' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('apf.workflow.wfTxn.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'apf.workflow.wfTxn.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.pidiscount.create',
					'state' : 'apf.finance.create',
					'params' : {}
				}]
			});
		}
		
		ctrl.openDisCountDetail = function(value) {
			modalService.openCustomDialogs({
				'dialogsModule' : 'apf.disccharge.detail',
				'params' : {
					"disCountDetail" : value,
					"loanCcy" : ctrl.piDiscountRequest.loanCcy
				}
			});
		};

		function submitFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
	}
})(angular);